/**
 * 
 */
package org.gov.vis.data.gui.listener;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.data.controller.ProcurementSemenCtlr;
import org.gov.vis.data.controller.ProcurementSemenCtlrImpl;
import org.gov.vis.data.dto.ProcurementSemenDto;
import org.gov.vis.data.gui.EditProcurementSemenDialog;
import org.gov.vis.data.gui.ViewProcurementSemenDialog;
import org.gov.vis.gui.util.ErrorsDisplayJPanel;
import org.gov.vis.main.Main;
import org.gov.vis.master.gui.listener.BaseListener;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.Util;

/**
 * @author user
 *
 */
public class EditProcurementSemenListener implements ActionListener,BaseListener{


	final private ResourceBundle rb = Util.getResources();

	private ProcurementSemenCtlr procurementSemenCtlr = new ProcurementSemenCtlrImpl();

	private EditProcurementSemenDialog editProcurementSemenDialog;

	private ErrorsDisplayJPanel errorMsgPanel;
	private ProcurementSemenDto procurementSemenDto;

	public EditProcurementSemenListener(EditProcurementSemenDialog editProcurementSemenDialog) {
		// TODO Auto-generated constructor stub
		this.editProcurementSemenDialog = editProcurementSemenDialog;
	}

	@Override
	public void clear() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setGuiDetailsToDto(Object _guiObject) {
		final EditProcurementSemenDialog _editProcurementSemenDialog = (EditProcurementSemenDialog)_guiObject;
		procurementSemenDto = new ProcurementSemenDto();
		procurementSemenDto.setCode(_editProcurementSemenDialog.getTxtFldCode().getText());
		procurementSemenDto.setBreed(_editProcurementSemenDialog.getTxtFldBreed().getText());
		procurementSemenDto.setBullNo(_editProcurementSemenDialog.getTxtFldBullNo().getText());
		procurementSemenDto.setDiscord(_editProcurementSemenDialog.getTxtFldDiscord().getText());
		if(_editProcurementSemenDialog.getDtChoDateOfReceipt().getDate() != null){
			procurementSemenDto.setDoRpt(_editProcurementSemenDialog.getDtChoDateOfReceipt().getDate());
		}
		procurementSemenDto.setFrmStation(_editProcurementSemenDialog.getTxtFldFromStation().getText());
		procurementSemenDto.setNoStraws(_editProcurementSemenDialog.getTxtFldNoOfStraws().getText());
		procurementSemenDto.setPrSemenId((Long)_editProcurementSemenDialog.getHidnObject());


	}

	@Override
	public void setDtoDetailsToGui(Object _dtoObject) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clearGUILabelColors(Color _labelColor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		// TODO Auto-generated method stub

		errorMsgPanel = new ErrorsDisplayJPanel(editProcurementSemenDialog.getjPanel1());
		errorMsgPanel.clearMsgPanel();


		if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_update"))){

			//Method called to either Add or Edit the GopalaMithra details
			addOrEditProcurementSemen();
		}

		if(actionEvent.getActionCommand().equals(rb.getString("dialog_button_delete"))){
			
			int response = JOptionPane.showConfirmDialog(null, GuiConstants.DELETE_CONFIRM_MSG, "Confirm",
			        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
			    
		    if (response == JOptionPane.YES_OPTION) {
		      // User clicked YES.
		    	deleteProcurementSemen(actionEvent);
		    	String searchText = editProcurementSemenDialog.getSearchText();

				// It is basically doing back button functionality. Please, understand the logic and write the comments.
				//Replace double quotes with appropriate Dialog title.
				ViewProcurementSemenDialog viewProcSemenDialog = new ViewProcurementSemenDialog(Main.getMainFrame(), "View Procurement Semen", false);
				for(ActionListener actionListener: viewProcSemenDialog.getSearchBtn().getActionListeners()) { 
					ViewProcurementSemenListener viewProcSemenList = (ViewProcurementSemenListener)actionListener;
					viewProcSemenDialog.getCodeTxtBx().setText(searchText);

					viewProcSemenList.actionPerformed(new ActionEvent(viewProcSemenDialog.getSearchBtn(), 1, rb.getString("dialog_button_serach")));
				}

		    	
		    } else if (response == JOptionPane.NO_OPTION || response == JOptionPane.CLOSED_OPTION) {
		      return;
		    }

		}
	}

	private void addOrEditProcurementSemen() {

		//Method called to set the Field's Labels to default Black color
		clearGUILabelColors(blackColor);

		//Method to set the Screen's data to  the DTO Object
		setGuiDetailsToDto(editProcurementSemenDialog);
		try{
			//Method called to Add or Edit the GopalaMithra
			procurementSemenCtlr.addOrEditPrSemen(procurementSemenDto);

			JOptionPane.showMessageDialog(editProcurementSemenDialog, rb.getString(GuiConstants.DATA_EDIT_PROCUREMENTSEMEN_SUCCESS));
			editProcurementSemenDialog.dispose();
			
			String searchText = editProcurementSemenDialog.getSearchText();

			// It is basically doing back button functionality. Please, understand the logic and write the comments.
			//Replace double quotes with appropriate Dialog title.
			ViewProcurementSemenDialog viewProcSemenDialog = new ViewProcurementSemenDialog(Main.getMainFrame(), "View Procurement Semen", false);
			for(ActionListener actionListener: viewProcSemenDialog.getSearchBtn().getActionListeners()) { 
				ViewProcurementSemenListener viewProcSemenList = (ViewProcurementSemenListener)actionListener;
				viewProcSemenDialog.getCodeTxtBx().setText(searchText);

				viewProcSemenList.actionPerformed(new ActionEvent(viewProcSemenDialog.getSearchBtn(), 1, rb.getString("dialog_button_serach")));
			}

		}catch(Exception e){
			List<String> errorMsgLst = new ArrayList<String>();
			if(e instanceof InvalidInputException){
				final HashMap<String,String> _exceptions = ProcurementSemenCtlr.errorMsgs;
				if (_exceptions != null && !_exceptions.isEmpty()) {
					String errMsg = null;

					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_semen_code").toUpperCase())){
						editProcurementSemenDialog.getLblCode().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_semen_code").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_semen_bullno").toUpperCase())){
						editProcurementSemenDialog.getLblBullNo().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_semen_bullno").toUpperCase());
						errorMsgLst.add(errMsg);
					}

					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_semen_frmstation").toUpperCase())){
						editProcurementSemenDialog.getLblFromStation().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_semen_frmstation").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_semen_noofStrwas").toUpperCase())){
						editProcurementSemenDialog.getLblNoOfStraws().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_semen_noofStrwas").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					if(_exceptions.containsKey(rb.getString("dialog_label_prmt_semen_dtofrcpt").toUpperCase())){
						editProcurementSemenDialog.getLblDateOfReceipt().setForeground(redColor);
						errMsg = _exceptions.remove(rb.getString("dialog_label_prmt_semen_dtofrcpt").toUpperCase());
						errorMsgLst.add(errMsg);
					}
					errorMsgPanel.setErrorMessages(errorMsgLst);					
				} else {
					//log.debug(e.getMessage(), e);
				}
			}
			else{
				errorMsgLst.add(e.getMessage());
				errorMsgPanel.setErrorMessages(errorMsgLst);
			}
		}		

	}

	private void deleteProcurementSemen(ActionEvent actionEvent) {

		Long procurementSemensId = (Long)editProcurementSemenDialog.getHidnObject();
		try {
			procurementSemenCtlr.deletePrSemen(procurementSemensId);
			JOptionPane.showMessageDialog(editProcurementSemenDialog, rb.getString(GuiConstants.DATA_DELETE_PROCUREMENTSEMEN_SUCCESS));
			editProcurementSemenDialog.dispose();
		} catch (Exception e) {

			List<String> errorMsgLst = new ArrayList<String>();
			errorMsgLst.add(e.getMessage());
			errorMsgPanel.setErrorMessages(errorMsgLst);

		}
	}

}//end class
